package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.AllocateMapper;
import com.ctgu.j08.mapper.BuyListMapper;
import com.ctgu.j08.mapper.DeviceMapper;
import com.ctgu.j08.service.AllocateService;
import com.ctgu.j08.service.BuyListService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.Allocate;
import com.ctgu.j08.vo.BuyList;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/6/2 19:34
 */
@Service
public class AllocateServiceImpl extends ServiceImpl<AllocateMapper, Allocate> implements AllocateService {
    @Autowired
    private AllocateMapper allocateMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    public Result editAllocate(Allocate allocate) {
        int isEdit = allocateMapper.updateByPrimaryKey(allocate);
        if(isEdit != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            System.out.println("更新失败"+isEdit);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addAllocate(Allocate allocate) {
        int isAdd = allocateMapper.insertSelective(allocate);
        //更新设备表
        int isUpdate = deviceMapper.updateStatusToYes(allocate.getDeId());
        if(isAdd != 0 && isUpdate !=0)
        {
            System.out.println("增加的分发信息："+allocate);
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllAllocateInfo() {
        List<Allocate> allocateList = allocateMapper.getAllAllocateInfo();
        if(allocateList != null)
        {
            System.out.println("分发信息列表："+allocateList);
            return ResultUtil.success(allocateList);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result delAllocate(Integer aId) {
        Allocate allocate = allocateMapper.selectByPrimaryKey(aId);
        //设备id
        Integer deId = allocate.getDeId();
        Integer isDel = allocateMapper.deleteByPrimaryKey(aId);
        //更新设备状态
        Integer isUpate = deviceMapper.updateStatusToNo(deId);
        if(isUpate !=0 && isDel != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
